package com.mike.spring.IOC;

import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.config.BeanFactoryPostProcessor;
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.core.io.ClassPathResource;

import java.text.ParseException;

public class App {
    public static void main(String[] args) throws ClassNotFoundException, ParseException {
        //xml方式
//        BeanFactory beanFactory = new XmlBeanFactory(new ClassPathResource("beans.xml"));
//        FXNewsProvider fxNewsProvider = (FXNewsProvider) beanFactory.getBean("fXNewsProvider");
//        FXNewsProvider fx = (FXNewsProvider) beanFactory.getBean("fx");
//        FXNewsProvider fxNews = (FXNewsProvider) beanFactory.getBean("fxNews");
//        fxNewsProvider.getNews();
//        fx.getNews();
//        fxNews.getNews();
//
//        //直接编码方式（最复杂最繁琐最底层）
//        DefaultListableBeanFactory beanFactory1 = new DefaultListableBeanFactory();
//        BeanDefinition daoJJhonesDefinition = new RootBeanDefinition(DaoJhonesNewsListener.class);
//        BeanDefinition newsListenerDefinition = new RootBeanDefinition(FXNewsProvider.class);
//        MutablePropertyValues propertyValues = new MutablePropertyValues();
//        propertyValues.addPropertyValue(new PropertyValue("newsListener", daoJJhonesDefinition));
//        beanFactory1.registerBeanDefinition("fXNewsProvider", newsListenerDefinition);
//        ((RootBeanDefinition) newsListenerDefinition).setPropertyValues(propertyValues);
//        FXNewsProvider fxNewsProvider1 = (FXNewsProvider) beanFactory1.getBean("fXNewsProvider");
//        fxNewsProvider1.getNews();
//
//        //外部配置文件方式(xml方式就是按照这种方式来实现的)
//        DefaultListableBeanFactory beanFactory3 = new DefaultListableBeanFactory();
//        XmlBeanDefinitionReader beanDefinitionReader = new XmlBeanDefinitionReader(beanFactory3);
//        beanDefinitionReader.loadBeanDefinitions(new ClassPathResource("beans.xml"));
//        FXNewsProvider fxNewsProvider3 = (FXNewsProvider) beanFactory3.getBean("fXNewsProvider");
//        fxNewsProvider3.getNews();
//        FXNewsProvider fxNewsProvider4 = (FXNewsProvider) beanFactory3.getBean("fx");
////        System.out.println(fxNewsProvider3.equals(fxNewsProvider4));
//
//
//        //annotation扫描方式
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("beans2.xml");
        HongKongNewsProvider provider4 = (HongKongNewsProvider) context.getBean("hongKongNewsProvider");
//        provider4.getNews();
//        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
//        FXNewsProvider provider4 = (FXNewsProvider)context.getBean("fXNewsProvider");
//        provider4.getNews();

//        ClassPathXmlApplicationContext context2 = new ClassPathXmlApplicationContext("beans2.xml",App.class);
//        HongKongNewsProvider provider5 = (HongKongNewsProvider) context2.getBean("hongKongNewsProvider");
//        provider5.getNews();

        //autowired
//        BeanFactory beanFactory4 = new XmlBeanFactory(new ClassPathResource("beans.xml"));
//        LogUtil logUtil =(LogUtil)beanFactory4.getBean("logUtil");
//        System.out.println(logUtil);
//
//
//        //静态工厂，非静态工厂，容器执行顺序
//        DefaultListableBeanFactory beanFactory5 = new XmlBeanFactory(new ClassPathResource("beans.xml"));
//        Foo foo = (Foo)beanFactory5.getBean("fooFactory");
//        System.out.println(foo);
//        Foo foo2 = (Foo)beanFactory5.getBean("fooStaticFactoryBean");
//        System.out.println(foo2);
//        /**
//         * BeanFactoryAware-->InitializingBean-->FactoryBean-->DisposableBean
//         */
//        Foo foo3 = (Foo) beanFactory5.getBean("fooFactoryBeanImpl");
//        System.out.println(foo3);
//        MyBeanFactoryPostProcessor myBeanFactoryPostProcessor = (MyBeanFactoryPostProcessor)beanFactory5.getBean("myBeanFactoryPostProcessor");
//        //ApplicationContext可以自动识别BeanFactoryPostProcessor但是BeanFactory需要下面这样手动配置
//        myBeanFactoryPostProcessor.postProcessBeanFactory(beanFactory5);
//        BeanFactoryPostProcessor beanFactoryPostProcessor = (BeanFactoryPostProcessor)beanFactory5.getBean("customEditorConfigurer");
//        BeanFactoryPostProcessor myBeanFactoryPostProcessor = (BeanFactoryPostProcessor)beanFactory5.getBean("myBeanFactoryPostProcessor");
//        beanFactoryPostProcessor.postProcessBeanFactory(beanFactory5);
//        myBeanFactoryPostProcessor.postProcessBeanFactory(beanFactory5);
//        DateObj dateObj = (DateObj) beanFactory5.getBean("dateObj");
//        System.out.println(dateObj);
//
//
//        //BeanFactoryPostProcessor(不能被BeanFactory调用只能被ApplicationConetxt调用)-->BeanFactoryAware(可以被Beanfactory调用)-->InitializingBean（可以被Beanfactory调用）-->FactoryBean-->DisposableBean
//        ApplicationContext applicationContext  = new ClassPathXmlApplicationContext("beans.xml");
//        applicationContext.getBean("fooFactoryBeanImpl");
//
//        //PropertyEditorSupport使用(CustomEditorConfigurer继承BeanFactoryPostProcessor，调用beanFactory.addPropertyEditorRegistrar添加DateEditor)
//        DateObj dateObj = (DateObj)applicationContext.getBean("dateObj");
//        System.out.println(dateObj);
//
//        //BeanWrapper使用
//        Class<DateObj> clzz = (Class<DateObj>) Class.forName("com.mike.spring.IOC.DateObj");
//        Date date = new SimpleDateFormat("YYYY-MM-DD").parse("2019-10-01");
//        BeanWrapper beanWrapper = new BeanWrapperImpl(clzz);
//        beanWrapper.setPropertyValue("date",date);
//        DateObj dateObj1 = (DateObj) beanWrapper.getWrappedInstance();
//        System.out.println(dateObj1);


    }
}
